package structure;

import java.util.ArrayList;

import algorithm.miscellaneous;

public class state {

	private ArrayList<item> items;
	private ArrayList<String> outgoing_arc;
	private ArrayList<String> ingoing_arc;
	private String label;

	public state(){
		
		this.items= new ArrayList<item>();
		this.outgoing_arc= new ArrayList<String>();
		this.ingoing_arc= new ArrayList<String>();
		this.label= "";
	}
	public state(ArrayList<item> items, String label){
		
		this.items= new ArrayList<item>();
		//this.items.addAll(items);
		//miscellaneous.addAllItemOnce(items, this.items);
		for(item i : items){
			
			if(!this.containsItem(i)){
				this.items.add(i);
			}
		}
				//miscellaneous.cloneArrayItem(items);
		this.outgoing_arc= new ArrayList<String>();
		this.createOutgoingArc();
		this.ingoing_arc= new ArrayList<String>();
		this.label= label;
	}
	
	public void createOutgoingArc(){
		
		ArrayList<String> toReturn= new ArrayList<String>();
		
		for(item i : this.items){
			
			String tmp= i.getAfterDot();
			if(!toReturn.contains(tmp))
				toReturn.add(tmp);
		}
		
		this.outgoing_arc.clear();
		this.outgoing_arc.addAll(toReturn);
	}
	
	public void setLabel(String label){
		
		this.label= "";
		this.label= label;
	}
	
	public ArrayList<item> getItems(){
		
		return this.items;
	}
	
	public item getItem(int i){
		
		return this.items.get(i);
	}
	
	public ArrayList<String> getOutgoing(){
		
		return this.outgoing_arc;
	}
	
	public String getLabel(){
		
		return this.label;
	}
	
	public ArrayList<item> getCompleteItems(){
		
		ArrayList<item> toReturn= new ArrayList<item>();
		
		for(item i : this.items){
			
			if(i.isComplete()){
				
				toReturn.add(i);
			}
		}
		
		return toReturn;
	}
	
	public boolean containsCompleteItem(){
		
		for(item i : this.items){
			
			if(i.isComplete())
				return true;
		}
		
		return false;
	}
	
	public boolean containsItem(item item){
		
		for(item i : this.items){
			
			if(i.sameAs(item))
				return true;
		}
		
		return false;
	}
	
	public boolean sameAs(state st){
		
		boolean toReturn= false;
			
			if(this.items.size() == st.getItems().size()){
				
				boolean same= true;
				for(int i=0; i<this.items.size(); i++){
					
					if(!this.items.get(i).sameAs(st.getItems().get(i))){
						
						same= false;
						break;
					}
				}
				
				toReturn= same;
			}
		
		
		return toReturn;
	}
	
	public void printItAll(){
		
		System.out.println("----------" + this.label + "----------");
		for(int index=0; index<this.items.size(); index++){
			
			item i= this.items.get(index);
			if(index>0 && index%4 == 0){

				i.printItAll();
				System.out.print("   \n");
			}
			else{

				i.printItAll();
				System.out.print("   ");
			}
		}
		System.out.println("\n" + this.outgoing_arc);
		System.out.println("");
	}
}
